//
//  LYMinePublishViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/9.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMinePublishViewController.h"
#import "LYFriendCell.h"
#import "LYPostDetailModel.h"

@interface LYMinePublishViewController ()<VVYRefreshTableViewDelegate, UITableViewDataSource,LYFriendCellDelegate>

@property (nonatomic, strong) NSMutableArray *models;

@property (nonatomic, assign) NSInteger page;

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation LYMinePublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self.tableView selector:@selector(reloadData) name:LYImageDownloadFinished object:nil];
    [self.tableView.mj_header beginRefreshing];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self.tableView];
    [self resignThemeNotification];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
    
    
    [LYRequestTool getMyPublicWithToken:LYToken page:@"1" per_page:@"10" success:^(NSArray<LYPostDetailModel *> *models) {
        
        [self.tableView.mj_header endRefreshing];
         [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;

        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
    }];
    
    
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    NSString *perpage = @"10";
    
    
    [LYRequestTool getMyPublicWithToken:LYToken page:page per_page:perpage success:^(NSArray<LYPostDetailModel *> *models) {
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_footer endRefreshing];
    }];

    
}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    LYFriendCell *friendCell = (LYFriendCell *)cell;
    
    LYPostDetailModel *model = self.models[indexPath.row];
    friendCell.model = model;
    friendCell.delegate = self;
    
}

#pragma mark - tableViewDataSource
/**
 *  cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYFriendCell *cell = [LYFriendCell cellWithTableView:tableView andIndetifier:LYFriendCellIdentifier];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.models.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYPostDetailModel *model = self.models[indexPath.row];
    return model.rowHeight;
}

#pragma mark - friendCellDelegate

- (void)cellShowContentBtnOnclick:(LYFriendCell *)cell{
    
    cell.model.showContent = !cell.model.showContent;
    [self.tableView reloadData];
}


@end
